//index.js
//获取应用实例
const app = getApp()

import {
  HTTP
} from '../../utils/http-p.js'
const http = new HTTP;

Page({
  onShareAppMessage() {
    return {
      title: 'scroll-view',
      path: 'page/component/pages/scroll-view/scroll-view'
    }
  },
  data: {
    productSuperiorList:[],
    condition:'1',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    value:'',
    toView: 'green',
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    expertList: [{ //假数据
      img: "avatar.png",
      name: "欢顔",
      tag: "知名情感博主",
      answer: 134,
      listen: 2234
    }],
    lunList:[],
    lunLists:[
      { img: 'http://139.155.16.228:8802/09-38-31.jpg'},
      { img: 'http://139.155.16.228:8802/09-39-43.jpg' },
      { img: 'http://139.155.16.228:8802/09-39-50.jpg' },
      { img: 'http://139.155.16.228:8802/09-39-53.jpg' }
    ],
    typeList:[]
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  goToSearch: function () {
    wx.navigateTo({ url: '/pages/searchShop/searchShop' })
  },
  goToSearchType: function (e) {
    console.log(e)
    let categoryType = e.currentTarget.dataset['categorytype'];
    console.log(categoryType)
    const type = '2'
    wx.navigateTo({ url: '/pages/searchShop/searchShop?isType='+type+'&categoryType='+categoryType })
  },

  goToPagePractice:function(e){
    let type = e.currentTarget.dataset['type'];
    wx.navigateTo({ url: '/pages/practice/practice?type='+type })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  onChange(e) {
    this.setData({
      value: e.detail
    });
  },

  goToShopInfo(event) {
    const productId = event.currentTarget.dataset.productid
    wx.navigateTo({
      url: `/pages/shopInfo/shopInfo?productId=${productId}`
    })
  },

  goToShopCar() {
    if (http.isLogin()) {
      return false
    }
    wx.navigateTo({ url: '/pages/shopCar/shopCar' });
  },

  /**
   * 查询轮播图
   */
  queryImgAll(){
    http.request({
      url: '/home-img',
      success: (res) => {
        const list = res.data;
        this.setData({
          lunList: list,
        })
      }
    })
  },
  onSearch() {
    Toast('搜索' + this.data.value);
  },

  onClick() {
    Toast('搜索' + this.data.value);
  },
  // 轮播图、
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
  },
  test:function(e){
    
    var cur = e.target.dataset.current;
    console.log(cur)
    this.setData({
      condition: cur
    })
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  queryTypeImg() {  
    http.request({
      url: '/home-category-img',
      success: (res) => {
        const list = res.data;
        this.setData({
          typeList: list,
        })
      }
    })
  },
  queryProductSuperior(){
    http.request({
      url: '/product-superior',
      success: (res) => {
        const list = res.data;
        this.setData({
          productSuperiorList: list,
        })
      }
    })
  },
  addVisitCount() {
    http.request({
      url: '/visit-count/addCount',
      success: (res) => {
      
      }
    })
  },
  onLoad: function (options) {
    this.queryImgAll()
    this.queryTypeImg()
    this.queryProductSuperior()
    this.addVisitCount()
  },
})
